package com.weimob.hystrix.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

@RestController
public class IndexController {

	/**
	 * 默认Restful接口
	 * @return
	 * @throws InterruptedException
	 */
    @RequestMapping("/index")
    public String index() throws InterruptedException {
        Thread.sleep(30);
        return "index";
    }
    
    /**
     * Hystrix保护接口，默认开启了熔断、隔离功能
     * 默认隔离：线程池
     * groupkey：IndexController
     * commandKey:keyUseMethod
     * 方法默认超时时间：1000ms
     * 没有失败回调方法
     * @return
     * @throws InterruptedException 
     */
    @HystrixCommand
    @RequestMapping("/hystrix/sample1")
    public String hystrixSample1(@RequestParam("param1")Integer sleepTime) throws InterruptedException {
    	Thread.sleep(sleepTime);
    	return "Hystrix sample 1";
    }
    
    /**
     * Hystrix保护接口、失败回掉方法
     * 配置groupKey、commandKey、线程池隔离、配置失败回调方法
     * 默认隔离：线程池
     * 默认超时时间：1000 ms
     * 熔断阈值：20次/10秒
     * 熔断休眠时间窗：5000 ms
     * @return
     * @throws InterruptedException 
     */
    @HystrixCommand(groupKey="indexController1", commandKey="hystrixSample2", fallbackMethod="hystrixSampleFallback")
    @RequestMapping("/hystrix/sample2")
    public String hystrixSample2(@RequestParam("param1")Integer sleepTime) throws InterruptedException {
    	if (sleepTime == null) {
    		throw new RuntimeException("Param Invalid");
    	} else {
    		Thread.sleep(sleepTime);
    	}
    	return "Hystrix sample 2";
    }
    
    /**
     * Hystrix保护接口、信号量模式
     * 配置groupKey、commandKey、线程池隔离、配置失败回调方法
     * 隔离：信号量
     * 信号量个数：2
     * 超时时间：1000ms
     * @return
     * @throws InterruptedException 
     */
    @HystrixCommand(groupKey="indexController2", commandKey="hystrixSample3", fallbackMethod="hystrixSampleFallbackFirst",
    		commandProperties= {
    				@HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE"),	// 设置为信号量模式
    				@HystrixProperty(name="execution.isolation.semaphore.maxConcurrentRequests", value="20")	//设置信号量个数为2，每秒最多2个请求
    		})
    @RequestMapping("/hystrix/sample3")
    public String hystrixSample3(@RequestParam("param1")Integer sleepTime) throws InterruptedException {
    	Thread.sleep(sleepTime);
    	return "Hystrix sample 3";
    }   
    
    
	public String hystrixSampleFallback(Integer sleepTime) {
    	return "Hystrix sample fallback";
    }
    
    @HystrixCommand(fallbackMethod="hystrixSampleFallbackSec")
	private String hystrixSampleFallbackFirst(Integer sleepTime) {
    	if (sleepTime > 500) {
    		throw new RuntimeException("Test Fall Back Second");
    	}
    	return "Hystrix sample fallback First";
    }
    
    @SuppressWarnings("unused")
	private String hystrixSampleFallbackSec(Integer sleepTime) {
    	return "Hystrix sample fallback Second";
    }
    
}
